package net.xt.testthread;

import java.util.concurrent.locks.ReentrantLock;

public class TestReentrantLock {

    public static void main(String[] args) {
        TestReentrantLock test = new TestReentrantLock();
        test.call(5);
    }

    public void call(int num) {
        ReentrantLock reentrantLock = new ReentrantLock(true);

        // 加锁
        reentrantLock.lock();
        try {
            if (num == 0) {
                return;
            }
            System.out.println(Thread.currentThread().getName() + ",num = " + num);

            // 递归调用
            // 此时锁没有释放，但是重入锁再次调用方法时会自动获取锁，不会因为之前锁没有释放而阻塞
            this.call(num - 1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            reentrantLock.unlock();
        }
    }

}
